from random import randint, random
from PyQt4.QtGui import QWidget, QLabel, QPixmap

class LEDLamp(QWidget):
	loaded = False
	leds = []

	def __init__(self, *args, **kw):
		QWidget.__init__(self, *args, **kw)

		if not LEDLamp.loaded:
			self.load_images()
			LEDLamp.loaded = True

		self.change_chance = 0.001 * randint(1,20)

		pic = QLabel(self)
		pic.setPixmap(QPixmap("work/led_lamp.png"))
		pic.setGeometry(0, 0, 18,12)

		pic = QLabel(self)
		pic.setPixmap(LEDLamp.leds[0])
		pic.setGeometry(5, 2, 8,8)

		self.pic = pic

		self.randomise()


	def load_images(self):
		leds = [
			"led_blue",	"led_brown",	"led_green",
			"led_grey",	"led_purple",	"led_red",
			"led_yellow",	"led_dark",	"led_dark",
			"led_dark"
		]

		for led in leds:
			file = "work/%s.png" % led
			LEDLamp.leds.append(QPixmap(file))


	def randled(self):
		return self.randel(LEDLamp.leds)

	def randel(self, list):
		lim = len(list)
		if lim == 0:
			return None
		return list[randint(0, lim-1)]

	def check(self):
		if random() < self.change_chance:
			self.randomise()

	def randomise(self):
		pix = self.randled()
		self.pic.setPixmap(pix)


